package com.example.smartcity_40.me

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.smartcity_40.R
import com.example.smartcity_40.databinding.ActivityPasswordBinding
import com.example.smartcity_40.tool.Tool
import com.example.smartcity_40.tool.tool
import com.example.smartcity_40.tool.viewBinding
import org.json.JSONObject

class PasswordActivity : AppCompatActivity() {
    private val vb by viewBinding(ActivityPasswordBinding::inflate)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(vb.root)
        vb.passwordTb.setOnClickListener {
            finish()
        }
        vb.passwordBtn.setOnClickListener {
            val data = """
                {
                "newPassword": "${vb.passwordNew.text}",
                "oldPassword": "${vb.passwordOld.text}"
                }
            """.trimIndent()
            tool.apply {
                send("/prod-api/api/common/user/resetPwd",Tool.PUT,data,true) {
                    if (it.contains("操作成功")) {
                        Toast.makeText(context,"操作成功",Toast.LENGTH_SHORT).show()
                        finish()
                    } else {
                        Toast.makeText(context,JSONObject(it).getString("msg"),Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }
}